<?php
abstract class Session {
    private static $session = null;
    private static $engineName = '';
    private static $memcacheServer = '';
    private static $memcacheConn = null;
    public static  $isStarted = false;
    protected  function __construct() {}

    private static function init() {
        $handlers = array('file', 'memcache');
        $engineName = strtolower(Config::get('SESSION_ENGINE'));
        self::$engineName = $engineName;
        if(null == self::$session) {
            if (in_array($engineName, $handlers)) {
                if ('memcache' == $engineName) {
                    $memcacheServer = Config::get('SESSION_SERVER');
                    self::$memcacheServer = $memcacheServer;
                    ini_set('session.save_handler', 'memcache');
                    ini_set('session.save_path', $memcacheServer);
                } else {
                    if (false == is_dir(SESS_DIR)) 
                        @mkdir(SESS_DIR, 0777, true);
                    ini_set('session.save_path', SESS_DIR);
                }
            } else {
                $driverName = ucfirst($engineName) . 'Session';
                $engineFile = PUMP_CORE . '/SessionDriver/' . $driverName . '.class.php';
                require_once($engineFile);
                self::$session = new $driverName();
                session_set_save_handler(array(&self::$session, 'open'),  
                                         array(&self::$session, 'close'),
                                         array(&self::$session, 'read'), 
                                         array(&self::$session, 'write'), 
                                         array(&self::$session, 'sessDestroy'), 
                                         array(&self::$session, 'gc'));
            }
        }
        $gcMaxLife = Config::get('SESSION_LIFETIME');
        $gcMaxLife = $gcMaxLife >= 86400 ? $gcMaxLife : 86400;
        ini_set('session.gc_maxlifetime', $gcMaxLife);
    }
    
    public static function start() {
        if (false == self::$isStarted) {
            self::init();
            session_start();
            $cookieLifeTime = Config::get('SESSION_LIFETIME') >0 ? time() + Config::get('SESSION_LIFETIME') : 0;
            setcookie(session_name(), session_id(), $cookieLifeTime, "/");
            self::$isStarted = true;
        }
    }

    public static function get($key) {
        if (false == self::$isStarted) self::start();
        return $_SESSION[$key];
    }

    public static function set($key, $value) {
        if (false == self::$isStarted) self::start();
        $_SESSION[$key] = $value;
    }
    
    
    
    #以下方法在子类中必须被继承 
    abstract public function open();
    abstract public function read();
    abstract public function write();
    abstract public function close();
    abstract public function sessDestroy();
    abstract public function gc();

    public static function destroy() {
        self::start();
        $handlers = array('file', 'memcache');
        if (false == in_array(self::$engineName, $handlers)) {
            self::$session->sessDestroy();
        } else {
            session_destroy();
            session_start();
        }
    }
    
    public static function delete($sessionId) {
        if (false == self::$isStarted) self::start();
        if ('file' == self::$engineName) {
            throw new Exception('PHP原生SESSION不支持删除指定SESSION');
        }
        if ('memcache' == self::$engineName) {
            
        } else {
            self::$session->delete($sessionId);
        }
    }
}
?>